<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\hasOne;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property int $id 
 * @property int $business_id 
 * @property int $category_id 
 * @property int $type 
 * @property string $title 
 * @property string $description 
 * @property array $tags
 * @property float $price 
 * @property float $goods_price 
 * @property float $reach_price 
 * @property float $rebate 
 * @property string $picture
 * @property array $pictures
 * @property int $sort 
 * @property int $expire_time 
 * @property int $expire_type 
 * @property int $status 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at
 * @property int $deleted_at 
 */
class Coupon extends Model
{
    use SoftDeletes;

    public $dateFormat = 'U';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'coupon';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'business_id' => 'integer', 'category_id' => 'integer', 'type' => 'integer', 'tags' => 'array', 'price' => 'float', 'goods_price' => 'float', 'reach_price' => 'float', 'rebate' => 'float', 'pictures' => 'array', 'sort' => 'integer', 'expire_time' => 'integer', 'expire_type' => 'integer', 'sales' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'integer'];

    /**
     * 关联优惠券库存
     *
     * @return HasMany
     */
    public function stock() : hasMany
    {
        return $this->hasMany(CouponStock::class, 'coupon_id', 'id');
    }

    /**
     * 关联优惠券店铺
     *
     * @return hasOne
     */
    public function business() : hasOne
    {
        return $this->hasOne(Business::class, 'id','business_id');
    }

    /**
     * 过期时间获取器
     *
     * @param $value
     * @return false|float|int|string
     */
    public function getExpireTimeAttribute($value)
    {
//        if ($this->expire_type === 1) {
//            return $value / 86400;
//        }
        if ($this->expire_type === 2) {
            return date('Y.m.d', $value);
        }

        return $value;
    }

    /**
     * 开始时间获取器
     *
     * @param $value
     * @return false|float|int|string
     */
    public function getStartTimeAttribute($value)
    {
//        if ($this->expire_type === 1) {
//            return $value / 86400;
//        }
        if ($this->expire_type === 2) {
            return date('Y.m.d', $value);
        }

        return $value;
    }

    /**
     * 时间获取器
     *
     * @param $value
     * @return int|string
     */
    public function getCreatedTimeAttribute($value)
    {
        return date('Y.m.d', $value);
    }
}